package net.xdclass.controller;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.controller.request.TrafficPageRequest;
import net.xdclass.controller.request.UseTrafficRequest;
import net.xdclass.service.TrafficService;
import net.xdclass.util.JsonData;
import net.xdclass.util.JsonUtil;
import net.xdclass.vo.TrafficVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {

    @Autowired
    private TrafficService trafficService;

    @Value("${rpc.token}")
    private String rpcToken;

    /**
     * 使用流量包API
     *
     * @return
     */
    @PostMapping("reduce")
    public JsonData useTraffic(@RequestBody UseTrafficRequest useTrafficRequest, HttpServletRequest request) {
        String requestToken = request.getHeader("rpc-token");
        if (requestToken.equalsIgnoreCase(rpcToken)) {
            //鉴权成功，执行逻辑
            //TODO 具体使用流量包逻辑
            JsonData jsonData = trafficService.reduce(useTrafficRequest);
            return jsonData;
        }
        return JsonData.buildError("非法访问！");
    }

    /**
     * 翻页查询
     *
     * @param request
     * @return
     */
    @RequestMapping("page")
    public JsonData page(@RequestBody TrafficPageRequest request) {

        Map<String, Object> pageMap = trafficService.pageAvailable(request);
        return JsonData.buildSuccess(pageMap);

    }

    /**
     * 查找某个流量包详情
     *
     * @param trafficId
     * @return
     */
    @GetMapping("/detail/{trafficId}")
    public JsonData detail(@PathVariable("trafficId") long trafficId) {

        TrafficVO trafficVO = trafficService.detail(trafficId);

        return JsonData.buildSuccess(trafficVO);
    }


}
